package com.bayu.common.lib;
/*
 * Created on Oct 11, 2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author By-U
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class OperasiFile {
	
	public OperasiFile(){
	
	}
	public OperasiFile(String pathFile,String isi,boolean mode){
		 buatFile(pathFile,isi,mode);
	}
	
	public void UbahFile(String src,String dest){
		File fubah = new File(src);
		
		
		if (fubah.isFile()){
			if(fubah.renameTo(new File(dest))){
				//System.out.println("Directory Berhasil Dicopy");
			}else{
				System.out.println("Gagal Rename "+src+" dest"+dest);
			}
		}
		
	}
	
	public PrintStream buatFile(String pathFile,boolean mode){
		PrintStream ps = null;
		try{
			FileOutputStream fos = new FileOutputStream(pathFile,mode);
			ps = new PrintStream(fos);
		}catch(IOException e){
			System.out.println("Failed Create a File");
		}
		
		return ps;
		
	
	}
	
	public void isiFile(PrintStream ps,String isiFile){
		ps.println(isiFile);
		//ps.close();
	}
	
	public void buatFile(String pathFile, String isi,boolean mode){
		PrintStream ps = null;
		StringTokenizer st;
		
		
		
		try{
			FileOutputStream fos = new FileOutputStream(pathFile,mode);
			ps = new PrintStream(fos);
			
			//Mengisi File
			if(isi.contains(",")){
				st = new StringTokenizer(isi,",");
				while(st.hasMoreTokens()){
					ps.println(st.nextToken());
					ps.close();
				}
			}else{
				if(mode == false){
					ps.print(isi);
					ps.close();
				}else{
					ps.println(isi);
					ps.close();
				}
				
			}
			
		
		}catch(IOException ie){
			System.out.println("Cannot Create Support File");
		}
		
		//ps.close();
				
	}
	
	public static String bacaFile(String namaFile){
		String strData = null;
		FileInputStream fis = null;
		
		File f = new File(namaFile);
		
		if(f.exists()){
			try{
				fis = new FileInputStream(namaFile);
				
			}catch(IOException ie){
				System.out.println("Cannot Read a File "+namaFile);
			}
			
			try{
				int fSize = fis.available() ;
				
				byte buffIn[] = new byte[fSize];
				
				int BytesRead = fis.read(buffIn,0,fSize);
				
				strData = new String(buffIn,0,fSize);
				
				fis.close();
				
			}catch(Exception e){
				System.out.println("Error...");
			}
		}else{
			strData="";
		}
		
		return strData;
	}
	
	public int dirSize(File f) {
		int size = 0;
		//File f = new File("E:\\Dataku\\programming\\J2SE\\sipuribeta\\server");
		File files[] = f.listFiles();
		try{
			for(int i=0;i< files.length ;i++){
				//System.out.println(files[i].toString());
				if(files[i].isDirectory()){
					size +=dirSize(files[i]);
				}else{
					size +=files[i].length();
				}
			}
		}catch(Exception e){
			System.out.println("Trying Calculate Directory");
		}
		return size;
	}
	
	public static ArrayList listFiles(String path){
		
		File f = new File(path);
		
		ArrayList hasil = new ArrayList();
		
		File files[] = f.listFiles();
		for(int i=0;i<files.length;i++){
			if(files[i].isFile()){
				hasil.add(files[i].getName());
			}
			
		}
		return hasil;
	}
	
	
	public static String[] listFiles2(String Path){
		ArrayList data= listFiles(Path);
		String[] strFile = new String[data.size()];
		
		for(int i=0;i<data.size();i++){
			strFile[i]=data.get(i).toString();
		}
		
		
		return strFile;
	}
	
	void copy(File src, File dst) throws IOException {
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dst);
    
        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
	
	public static void hapusFile(String namaFile){
		File f = new File(namaFile);
		if(f.exists()){
			f.delete();
		}else{
			
		}
	}

	
	
}
